 jQuery(document).ready(function($) {


   var post_button = $('#post_button');

///POST MESSAGE////
  postClick();
     
   function postClick(){
     post_button.removeClass('disabled');
     post_button.click(function(){
        var message = $('#message').val();
        if(message == '') return;
        postMessage(message);
        $(this).unbind();
        $(this).addClass('disabled');
     });
   };


   function postMessage(message){

        $.ajax({
          url: base_url + "post/post_message",
          type: "POST",
          data: {message :message},
          success: function(data){
           var post = $(data);
           post.insertBefore(".item:eq(0)");
           post.hide().slideDown();
           $('#message').val("");
           postClick();

          }
        });
   }


////DELETE MESSAGE////
     $(".post_options .delete").live("click", function(e){
           e.stopImmediatePropagation();
           $(this).closest('.item').find('.post_options .delete_confirmation').slideDown();
     });
     $(".post_options .delete_confirmation .yes").live("click", function(e){
         e.stopImmediatePropagation();
         $this = $(this);
         var id = $this.closest('.post_options').find('.delete').attr('id');

         $this.unbind();

        $.ajax({
          url: base_url + "post/delete/"+id,
          type: "GET",
          succes:function(){
              alert('WOOHOOOOO');
          }

        });



        var message = $this.closest('.item');
         animateOut(message);

     });

     $(".item").live("mouseover", function(e){
         $(this).addClass("hover");
     });

     $(".item").live("mouseout", function(e){
         if($(this).find('.delete_confirmation:visible').size() == 0){
              $(this).removeClass("hover");
         }
     });



      $(".post_options .delete_confirmation").live("click", function(e){
          e.stopImmediatePropagation();
      });





      $(".post_options .delete_confirmation .no").live("click", function(e){
          e.stopImmediatePropagation();
           $(this).closest('.item').find('.post_options .delete_confirmation').fadeOut(300);
      })

     $(document).click(function(){
       
        $(".post_options:visible .delete_confirmation .no").click();
     })

     function animateOut(message){
         message.animate({opacity:0.01}, {duration:500, complete:function(){
            $(this).slideUp(500, function(){
                $(this).remove();
            });
         }
         });
     }

});